import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/',
    component: () => import('@/pages/index'),
  },
  {
    path: '/tv/:id',
    component: () => import('@/pages/tv'),
    meta: {
      keepAlive: true // 需要缓存
    }
  },
  {
    path: '/404',
    component: () => import('@/pages/404'),
  },
  { path: '*', redirect: '/404' },
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior (to, from, savedPosition) {
    // console.log(to, from, savedPosition)
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  routes: constantRoutes,
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
